<template>
  <component 
    :is="tag"
    :class="[
      'bx-icon',
      {
        'bx-icon--spin': spin,
        'bx-icon--loading': loading
      }
    ]"
    :style="iconStyle"
    @click="handleClick"
  >
    <svg 
      v-if="name"
      :viewBox="viewBox"
      :width="size"
      :height="size"
      fill="currentColor"
    >
      <use :href="`#icon-${name}`" />
    </svg>
    <slot v-else />
  </component>
</template>

<script>
import { computed } from 'vue'

export default {
  name: 'BxIcon',
  props: {
    name: {
      type: String,
      default: ''
    },
    size: {
      type: [String, Number],
      default: '1em'
    },
    color: {
      type: String,
      default: ''
    },
    tag: {
      type: String,
      default: 'i'
    },
    spin: {
      type: Boolean,
      default: false
    },
    loading: {
      type: Boolean,
      default: false
    },
    viewBox: {
      type: String,
      default: '0 0 24 24'
    }
  },
  emits: ['click'],
  setup(props, { emit }) {
    const iconStyle = computed(() => {
      const style = {}
      
      if (props.size) {
        const size = typeof props.size === 'number' ? `${props.size}px` : props.size
        style.fontSize = size
        style.width = size
        style.height = size
      }
      
      if (props.color) {
        style.color = props.color
      }

      return style
    })

    const handleClick = (event) => {
      emit('click', event)
    }

    return {
      iconStyle,
      handleClick
    }
  }
}
</script>

<style scoped>
.bx-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-style: normal;
  line-height: 0;
  text-align: center;
  text-transform: none;
  vertical-align: -0.125em;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  cursor: pointer;
  transition: all var(--bx-transition-medium);
}

.bx-icon svg {
  display: block;
  width: 1em;
  height: 1em;
  fill: currentColor;
}

.bx-icon--spin {
  animation: bx-icon-spin 1s linear infinite;
}

.bx-icon--loading {
  animation: bx-icon-spin 1s linear infinite;
}

@keyframes bx-icon-spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.bx-icon:hover {
  opacity: 0.8;
}
</style>

